package com.xiaorun.portal.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.controller.BaseController;
import com.xiaorun.portal.exception.TipException;
import com.xiaorun.portal.modal.Bo.RestResponseBo;
import com.xiaorun.portal.modal.Vo.Case;
import com.xiaorun.portal.service.ICaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 13 on 2018/2/21.
 */
@Controller()
@RequestMapping("admin/case")
@Slf4j
public class CaseController extends BaseController {

    @Autowired
    private ICaseService caseService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        return this.list(1, 10, params, request);
    }

    @RequestMapping(value = "/list/{pageNum}/{pageSize}")
    public String list(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        PageInfo<Case> page = caseService.list(pageNum, pageSize, params);
        request.setAttribute("cases", page);
        return "admin/case_list";
    }

    @RequestMapping(value = "/search")
    public String search( String type, String subtype, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("type", type);
        params.put("subtype", subtype);
        return this.list(1, 10, params, request);
    }

    @GetMapping(value = "/add")
    public String add(HttpServletRequest request) {
        return "admin/case_edit";
    }

    @GetMapping(value = "/edit/{id}")
    public String editPage(@PathVariable Integer id, HttpServletRequest request) {
        Case cas = caseService.get(id);
        request.setAttribute("case", cas);
        return "admin/case_edit";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo save(Case cas, HttpServletRequest request) {
        try {
            cas.setAuthorId(this.getUid(request));
            caseService.save(cas);
        } catch (Exception e) {
            String msg = "新增失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            }
            log.error(msg, e);
            return RestResponseBo.fail(msg);
        }

        return RestResponseBo.ok();
    }

    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@PathVariable int id, HttpServletRequest request) {
        try {
            caseService.remove(id);
        } catch (Exception e) {
            String msg = "删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            }
            log.error(msg, e);
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }

}
